// File Name:	"CGame.cpp"
// Author:		Phil Sullivan
// Purpose:		To encapsulate all game-related code

#include "CGame.h"
#include "Game States\CMainMenuState.h"
#include "Game States\CGamePlayState.h"
//#include "Game States/IGameState.h"


CGame::CGame()
{
	m_pD3D = NULL;
	m_pTM = NULL;
	m_pDI = NULL;
	m_pFM = NULL;
	
	//Assets
	m_nImageID = -1;
	m_nSoundID = -1;
	m_pCurrentState = NULL;
	
	m_nScnHeight = -1;
	m_nScnWidth = -1;
	
	//TimeBased Update
	m_dwTimeStamp = 0;
	m_dwPrevTimeStamp = 0;
	m_fElapsedTime = 0;
	m_fGameTime = 0;

	m_nFrameCount	= 0;
	m_nFPS			= 0;
	
	m_bIsFullScreen = false;
	// m_bIsPaused;
	
	fVolume, fSFX, fPan = .5f;

}

CGame::~CGame()
{
}


bool CGame::Main()
{
	DWORD dwStartTimeStamp = GetTickCount();
	m_fElapsedTime = (float)(dwStartTimeStamp - m_dwPrevTimeStamp) / 1000.f;
	m_dwPrevTimeStamp = dwStartTimeStamp;

	//1. Input
	if(Input() == false)
		return false;
	
	//2. Update
	Update();

	//3. Render
	Render();


		m_nFrameCount++;

	//	Check if 1 second has passed
	if (GetTickCount() - m_dwTimeStamp > 1000)
	{
		//	Remember the frame count
		m_nFPS = m_nFrameCount;
		//	Reset the frame count
		m_nFrameCount = 0;
		//	Get a new time stamp
		m_dwTimeStamp = GetTickCount();
	}


	return true;
}

//3. Shut down all relevant game code
void CGame::ShutDown()
{

}

//2. Execution - returns false when we want to quit
void CGame::Initialize(HWND hWnd, HINSTANCE hInstance, int nScWidth, int nScHeight, bool bIsWindowed, bool bVsync)
{
	m_pD3D = CSGD_Direct3D::GetInstance();
	m_pTM = CSGD_TextureManager::GetInstance();
	m_pDI = CSGD_DirectInput::GetInstance();
	m_pFM = CSGD_FModManager::GetInstance();

	m_pD3D->InitDirect3D(hWnd, nScWidth, nScHeight, bIsWindowed, false);
	m_pTM->InitTextureManager(m_pD3D->GetDirect3DDevice(), m_pD3D->GetSprite());
	m_pDI->InitDirectInput(hWnd, hInstance, DI_KEYBOARD | DI_MOUSE | DI_JOYSTICKS);
	
	m_pFM->InitFModManager(hWnd);


	m_nScnHeight = nScHeight;
	m_nScnWidth = nScWidth;

	m_dwTimeStamp = GetTickCount();
	m_dwPrevTimeStamp = GetTickCount();
	ChangeState(CMainMenuState::GetInstance());

}

bool CGame::Input()
{	
	m_pDI->ReadDevices();
	
	//int x = m_pDI->JoystickCheckBufferedButtons();

	//if( x != -1 )
	//{
	//	int y = 0;
	//}

	if ((m_pDI->KeyDown(DIK_LALT) || m_pDI->KeyDown(DIK_RALT)) && m_pDI->KeyPressed(DIK_RETURN))
	{
		m_bIsFullScreen = !m_bIsFullScreen;
		m_pD3D->ChangeDisplayParam(m_nScnWidth, m_nScnHeight,  m_bIsFullScreen);
		return true;			   
	}

	if(m_pDI->KeyDown(DIK_LALT) && m_pDI->KeyPressed(DIK_R)|| m_pDI->KeyDown(DIK_RALT) && m_pDI->KeyPressed(DIK_R))
	{
		ChangeState(CMainMenuState::GetInstance());
	}
	if( m_pCurrentState == CGamePlayState::GetInstance())
	{
		if(m_pDI->KeyPressed(DIK_P) || m_pDI->KeyPressed(DIK_ESCAPE) ||
			m_pDI->KeyPressed(DIK_ESCAPE) && m_pDI->KeyPressed(DIK_P) || m_pDI->JoystickButtonPressed(9) )
		{
			CGame::SetPause( !GetPause() );
		}
	}

		
	if( !m_bIsPaused )
	{
		if (!m_pStates[m_pStates.size()-1]->Input())
		{
			ChangeState(NULL);
			return false;
		}
	}
	return true;
}

void CGame::Update()
{
	if( !m_bIsPaused )
		m_pStates[m_pStates.size()-1]->Update(m_fElapsedTime);
	//m_pCurrentState->Update(m_fElapsedTime);
}

void CGame::Render()
{


	//char buffer[20];
	//
	//sprintf_s(buffer, _countof(buffer), "FPS: %i", 
	//							m_nFPS);

	//m_pD3D->DrawTextA(buffer, 10, 10);
	//m_pCurrentState->Render();	
	m_pStates[m_pStates.size()-1]->Render();




}

void CGame::ChangeState(IGameState* pNewState)
{
	if (m_pStates.size() > 1)
	{
		if( m_pCurrentState )
		{
			m_pCurrentState->Exit();
			m_pStates.pop_back();
		}
	}

	m_pCurrentState = pNewState;


	if (m_pCurrentState)
	{
		m_pStates.push_back(m_pCurrentState);
		m_pStates[m_pStates.size()-1]->Enter();
	}
}